var p;

var evaStore = new Ext.data.JsonStore({
//	url : "TryServlet?cmd=list&UorS=u",
	url:"TryServlet",
	baseParams:{cmd:"list",UorS:"U"},//EXT3.0新变化，params改为baseParams!
//	baseParams: {
//	action: "searchHistory",
//	serialNumber: 1
//	},
//	data:{success:true, data:[{Lp:"100"}]},
	totalProperty : 'totalProperty',
	root : "data",
	// fields: ["Ia", "udpT"]
	fields : [{name:"Eva_Time"}, "Lp", "Bw", "M", "synSa", "synDelay", "Lt", "Ir",
			"lambdaA", "Ia", "udpTt", "synPb", "Eva"]
		// remoteSort:true
	});

var resultRecord = new Ext.data.Record.create([//record字段描述
{name:'Eva_Time'},//时间
{name:'Lp'},//
{name:'Bw'},//
{name:'M'},//
{name:'synSa'},//
{name:'synDelay'},//
{name:'Lt'},//
{name:'Ir'},//
{name:'lambdaA'},//攻击强度(攻击包到达率)
{name:'Ia'},//攻击请求到达间隔
{name:'udpTt'},
{name:'synPb'},
{name:'Eva'}
//{name:'udpE'},//UDP Flood攻击绩效
//{name:'synE'}//SYN Flood攻击绩效
//{name:'HybridE'}//混合攻击绩效
]);
var eGridPanel;//结果表
var udpWColModel = new Ext.grid.ColumnModel([//用于eGridPanel的列模型
{
				width : 80,
				sortable : true,
				dataIndex : "Eva_Time",
				header : "Eva_Time(s)"
			},{
				width : 50,
				sortable : true,
				dataIndex : "lambdaA",
				header : "λa(次/s)"
			},{
				width : 50,
				sortable : true,
				dataIndex : "Bw",
				header : "Bw(Bytes)"
			},{
				width : 50,
				sortable : true,
				dataIndex : "Lp",
				header : "Lp(s)"
			},{
				width : 50,
				sortable : true,
				dataIndex : "Lt",
				header : "Lt(Bytes)"
			},{
				width : 50,
				sortable : true,
				dataIndex : "Ir",
				header : "Ir(s)"
			},{
				width : 50,
				sortable : true,
				dataIndex : "Ia",
				header : "Ia(s)"
			},{
				width : 50,
				sortable : true,
				dataIndex : "udpTt",
				header : "udpTt(s)"
			},{
				width : 50,
				sortable : true,
				dataIndex : "Eva",
				header : "绩效(s)"
			}
]);
var synWColModel = new Ext.grid.ColumnModel([{//用于eGridPanel的列模型
			width : 50,
			sortable : true,
			dataIndex : "Eva_Time",
			header : "Eva_Time(s)"
		},{
			width : 50,
			sortable : true,
			dataIndex : "lambdaA",
			header : "λa(次/s)"
		},{
			width : 50,
			sortable : true,
			dataIndex : "Lp",
			header : "Lp(Bytes)"
		},{
				width : 50,
				sortable : true,
				dataIndex : "Bw",
				header : "Bw"
			},{
			width : 50,
			sortable : true,
			dataIndex : "M",
			header : "M"
		},{
			width : 50,
			sortable : true,
			dataIndex : "synSa",
			header : "半开超时(s)"
		},{
			width : 50,
			sortable : true,
			dataIndex : "synDelay",
			header : "延时(s)"
		},{
			width : 50,
			sortable : true,
			dataIndex : "Ir",
			header : "Ir(s)"
		},{
			width : 50,
			sortable : true,
			dataIndex : "Ia",
			header : "Ia(s)"
		},{
			width : 50,
			sortable : true,
			dataIndex : "synPb",
			header : "Pb"
		},{
			width : 60,
			sortable : true,
			dataIndex : "Eva",
			header : "绩效(s)"
		}
		
]);

var evaChart = new Ext.chart.LineChart({//绩效图
		store:evaStore,
//		autoHeight:true,
//		autoWidth:true,
//		store:Ext.getCmp("udpGridPanel").store,
		xField:'Ia',
		yField:'Eva'
//		series:[
//			{
//			displayName: 'UDP Flood攻击绩效',
//			yField:'udpTt'
//		}
//		,{
//			displayName: 'SYN Flood攻击绩效',
//			yField:'synPb'
//		}
//		]
	}
	)

function handleAjaxFailure()
{
            Ext.MessageBox.show({
				title : 'AJAX错误',
				msg : "AJAX Service Error, Please see administrator",
				buttons : Ext.MessageBox.OK,
				icon : Ext.MessageBox.ERROR
			});
}

function requestServlet(theUrl) {//提交请求
	Ext.Ajax.request({
				url : theUrl,
				method : "GET",
				success : function(result, request) {
					var res = Ext.util.JSON.decode(result.responseText);
					if (res.success) {
						Ext.MessageBox.show({
									title : '请求成功',
									msg : res.msg,
									buttons : Ext.MessageBox.OK
								});
					} else {
						Ext.MessageBox.show({
									title : '保存失败',
									msg : res.msg,
									buttons : Ext.MessageBox.OK,
									icon : Ext.MessageBox.ERROR
								});
					}
				},
				failure : function(result, request) {
					Ext.MessageBox.show({
								title : 'AJAX错误',
								msg : "AJAX Service Error, Please see administrator",
								buttons : Ext.MessageBox.OK,
								icon : Ext.MessageBox.ERROR
							});
				}// failure
			})
}

//function requestUdpW(){
//	    Ext.Ajax.request({
//        url: 'TryServlet?cmd=udp_w',
//		method:"GET",
//        success: function(result, request) { 
//                            var res = Ext.util.JSON.decode(result.responseText);
//                            if (res.success){
//                               Ext.MessageBox.show({
//                                   title: '保存成功',
//                                   msg: res.msg,
//                                   buttons: Ext.MessageBox.OK
//                               });
//							}
//							else{
//								Ext.MessageBox.show({
//                                   title: '保存失败',
//                                   msg: res.msg,
//                                   buttons: Ext.MessageBox.OK,
//                                   icon: Ext.MessageBox.ERROR
//                               });
//						}},
//        params: {
//            lambdaA: lambdaA,
//            Ia: Ia,
//            udpT: udpT,
//			synPb:synPb,
//			udpE:udpE,
//			synE:synE
//        }
//    });
//}

//function saveRecord(lambdaA, Ia, udpT, synPb, udpE, synE){
//    Ext.Ajax.request({
//        url: 'TryServlet?cmd=save',
//		method:"GET",
//        success: function(result, request) { 
//                            var res = Ext.util.JSON.decode(result.responseText);
//                            if (res.success){
//                               Ext.MessageBox.show({
//                                   title: '保存成功',
//                                   msg: res.msg,
//                                   buttons: Ext.MessageBox.OK
////                                   icon: Ext.MessageBox.ERROR
//                               });
//							}
//							else{
//								Ext.MessageBox.show({
//                                   title: '保存失败',
//                                   msg: res.msg,
//                                   buttons: Ext.MessageBox.OK,
//                                   icon: Ext.MessageBox.ERROR
//                               });
//						}},
//						failure:handleAjaxFailure,
//        params: {
//            lambdaA: lambdaA,
//            Ia: Ia,
//            udpT: udpT,
//			synPb:synPb,
//			udpE:udpE,
//			synE:synE
//        }
//    });
////	requestServlet("save");
//}

//求阶乘函数
function factorial(n){
	if(n == 0){
		return 1;
	}
	else if(n<0){
		return;//javascript可用try catch处理错误 略
	}
	else{
		return n*factorial(n-1);
	}
}

//计算绩效
function calcE(Tt, Pb, Ti)
{
	if(Tt==Infinity){	//Tt<0时(UDP Flood攻击造成队列无限延长)绩效值无穷
		return Infinity;
	}
	else if (Pb<0||Pb>1){
		Ext.MessageBox.alert("Pb应在0到1之间!","Pb="+Pb.toString());
		return;
	}
	else if(Pb==1||Tt<0){//Pb为1时绩效值无穷
		
		return Infinity;
	}
	var E = Tt+Pb*Ti/(1-Pb);
//	return parseFloat(E.toPrecision(3));
	return parseFloat(E)
}



//计算UDP Flood攻击中的平均停留时间
// Lp Bytes, Bw Mbps, Ir s, Ia s
function udpCalcT(Lp, Bw, Ir, Ia){
	var mu = 1000000*Bw/8/Lp;
	var lambdaR = 1/Ir;
	var lambdaA = 1/Ia;
	var rho = (lambdaR + lambdaA)/mu;
	if(rho>=1){
//		Ext.MessageBox.alert("λ应小于μ!");
		return Infinity;
	}
	var Ts = 1/mu;
	var Tq = rho*Ts/2/(1-rho);
	var T = Tq+Ts;
	return T;
}

//计算SYN Flood造成的拒绝服务概率
// Sa s, delay s, Ir s, Ia s
//function synCalcPb(m, Sa, delay, Ir, Ia) {
function synCalcPb(m, Sa, Sr, Ir, Ia) {
//	var Sr = delay * 2;
	var rhoR = Sr / Ir;
	var rhoA = Sa / Ia;
	var G = 0, Pb = 0;

	for (var r = 0; r <= m; r++) {
		for (var a = 0, tmp = 0; a <= m - r; a++) {
			tmp += Math.pow(rhoA, a) / factorial(a);
		}
		G += Math.pow(rhoR, r) / factorial(r) * tmp;

	}
	for (var r = 0; r <= m; r++) {
		Pb += Math.pow(rhoR, r) / factorial(r) * Math.pow(rhoA, m - r)
				/ factorial(m - r);
	}

	var Pb = Pb / G;
	return Pb;
}

function udpW(){
//	Ext.MessageBox.alert("sadf");
	Ext.getCmp("udpTextT").setValue("正在计算...");
	var udpLp = parseFloat(Ext.getCmp("udpTextLp").getValue());
	var udpBw = parseFloat(Ext.getCmp("udpTextBw").getValue());
	var udpIr = parseFloat(Ext.getCmp("udpTextIr").getValue());
	var udpIa = parseFloat(Ext.getCmp("udpTextIa").getValue());
	var udpLt = parseFloat(Ext.getCmp("udpTextLt").getValue());//一次会话的传输总量
	var udpT = udpCalcT(udpLp, udpBw, udpIr, udpIa);
	//一次会话的平均停留总时间Tt
	var udpTt = udpT*udpLt*1000000/udpLp;
	Ext.getCmp("udpTextT").setValue(udpT.toString());
	var udpE = calcE(udpTt,0,0);
//	Ext.getCmp("eGridPanel").store.add(new resultRecord({Ia:udpIa, udpT:udpT, synPb:1}));
//		udpStore.add(new resultRecord({Ia:udpIa, udpT:udpT.toString(), synPb:1}));
	evaChart.xField = 'Ia';
	evaChart.Field = 'Eva';
//	evaStore.removeAll();
//	evaStore.add(new resultRecord({Ia:udpIa.toString(), udpTt:udpTt.toString(), Eva:udpE.toString()}));
	evaStore.add(new resultRecord({Ia:udpIa, udpTt:udpTt, Eva:udpE}));
//	evaStore.add(new resultRecord({Ia:1,Eva:5}));
	Ext.getCmp("udpTextE").setValue(udpE.toString());
//	Ext.getCmp("panelChart").render();
}
function synW(){//点击计算按钮后调用此函数
	Ext.getCmp("synTextPb").setValue("正在计算...");

	var synM = parseFloat(Ext.getCmp("synTextM").getValue());
	var synSa = parseFloat(Ext.getCmp("synTextSa").getValue());
	var synDelay = parseFloat(Ext.getCmp("synTextDelay").getValue());
	var synIr = parseFloat(Ext.getCmp("synTextIr").getValue());
	var synIa = parseFloat(Ext.getCmp("synTextIa").getValue());
	var synPb = synCalcPb(synM, synSa, synDelay * 2, synIr, synIa);
	var synTi = parseFloat(Ext.getCmp("synTextTi").getValue());
	var synE = calcE(0, synPb, synTi);// TODO
	Ext.getCmp("synTextPb").setValue(synPb.toString());
	Ext.getCmp("synTextE").setValue(synE.toString());
	evaChart.xField = 'Ia';
	evaChart.Field = 'Eva';
	evaStore.add(new resultRecord({Ia:synIa, synPb:synPb, Eva:synE}));

}

function saveUdpW(){//点击保存按钮后调用此函数
	var udpLp = parseFloat(Ext.getCmp("udpTextLp").getValue());
	var udpBw = parseFloat(Ext.getCmp("udpTextBw").getValue());
	var udpIr = parseFloat(Ext.getCmp("udpTextIr").getValue());
	var udpIa = parseFloat(Ext.getCmp("udpTextIa").getValue());
	var udpLt = parseFloat(Ext.getCmp("udpTextLt").getValue());//一次会话的传输总量
	var udpT =  parseFloat(Ext.getCmp("udpTextT").getValue());
	var udpE =  parseFloat(Ext.getCmp("udpTextE").getValue());
	var udpTt = udpT*udpLt*1000000/udpLp;
	//TryServlet?cmd=save&UorS=u&lp=1&bw=1&lt=1&ia=1&ir=1&udptt=1&eva=1
	var url = "TryServlet?cmd=save&UorS=u&lp="+ udpLp+ "&bw=" + udpBw + "&lt="+udpLt+"&ia="+udpIa+"&ir="+udpIr+"&udptt="+udpTt+"&eva="+udpE;
	requestServlet(url);
}
function saveSynW(){
	var synM = parseFloat(Ext.getCmp("synTextM").getValue());
	var synSa = parseFloat(Ext.getCmp("synTextSa").getValue());
	var synDelay = parseFloat(Ext.getCmp("synTextDelay").getValue());
	var synIr = parseFloat(Ext.getCmp("synTextIr").getValue());
	var synIa = parseFloat(Ext.getCmp("synTextIa").getValue());
//	var synPb = synCalcPb(synM, synSa, synDelay * 2, synIr, synIa);
	var synTi = parseFloat(Ext.getCmp("synTextTi").getValue());
//	var synE = calcE(0, synPb, synTi);// TODO
	var synPb = parseFloat(Ext.getCmp("synTextPb").getValue());
	var synE = parseFloat(Ext.getCmp("synTextE").getValue());
//	TryServlet?cmd=save&UorS=s&lp=1&bw=1&m=1&synsa=1&syndelay=1&lt=1&ia=1&ir=1&ti=1&udptt=1&synpb=1&eva=1
//	TryServlet?cmd=save&UorS=s&m=1&synsa=1&syndelay=1&ia=1&ir=1&ti=1&synpb=1&eva=1
	var url = "TryServlet?cmd=save&UorS=s&m="+ synM+ "&synsa=" + synSa + "&syndelay="+synDelay+"&ia="+synIa+"&ir="+synIr+"&ti="+synTi+"&synpb="+synPb+"&eva="+synE;
	requestServlet(url);
}

function listHistoryUdpW(){
	evaStore.setBaseParam("UorS", "u");
	evaStore.reload();
}

function listHistorySynW(){
//	evaStore.removeAll();
//	evaStore.proxy.url = "TryServlet?cmd=list&UorS=s";
//	evaStore.reload({baseParams:{cmd:"list",UorS:"s"}});
	evaStore.setBaseParam("UorS", "s");
	evaStore.reload();
}

//var udpStore = new Ext.data.Store()
Ext.onReady(function() {
	// //Panel用法
	p = new system.tryPerformance();
//	saveRecord(1,1,1,1,1,1);
//	requestServlet("TryServlet?cmd=save&UorS=u&lp=1000&bw=4&lt=5&ia=0.01&ir=0.5&udptt=10&eva=11.281");
//	evaStore.load();

//	绩效结果Grid
	eGridPanel = new Ext.grid.GridPanel({
//			id : "eGridPanel",
			region : "east",
			store:evaStore,
			colModel : udpWColModel,
			autoWidth : true,
			selModel : new Ext.grid.RowSelectionModel({}),
			height:200//用autoHeight不能显示scroll bar
		});
		
//	Ext.getCmp("panelGrid").add(new Ext.grid.GridPanel({
//			id : "eGridPanel",
//			region : "east",
//			store:evaStore,
//			columns : [{
//				hidden : false,
//				sortable : true,
//				dataIndex : "Eva_Time",
//				header : "Eva_Time(s)"
//			},{
//				hidden : false,
//				sortable : true,
//				dataIndex : "Ia",
//				header : "Ia(s)"
//			},{
//				hidden : false,
//				sortable : true,
//				dataIndex : "Bw",
//				header : "Bw(Bytes)"
//			},{
//				hidden : false,
//				sortable : true,
//				dataIndex : "Lp",
//				header : "Lp(s)"
//			}, {
//				hidden : false,
//				sortable : true,
//				dataIndex : "M",
//				header : "M"
//			},{
//				hidden : false,
//				sortable : true,
//				dataIndex : "synSa",
//				header : "synSa(s)"
//			},{
//				hidden : false,
//				sortable : true,
//				dataIndex : "synDelay",
//				header : "synDelay"
//			},{
//				hidden : false,
//				sortable : true,
//				dataIndex : "Lt",
//				header : "Lt(Bytes)"
//			},{
//				hidden : false,
//				sortable : true,
//				dataIndex : "Ir",
//				header : "Ir(s)"
//			},{
//				hidden : false,
//				sortable : true,
//				dataIndex : "lambdaA",
//				header : "λa(次/s)"
//			},{
//				hidden : false,
//				sortable : true,
//				dataIndex : "udpT",
//				header : "udpT(s)"
//			},{
//				hidden : false,
//				sortable : true,
//				dataIndex : "synPb",
//				header : "synPb"
//			},{
//				hidden : false,
//				sortable : true,
//				dataIndex : "M",
//				header : "M"
//			}
//			],
//			autoWidth : true,
//			selModel : new Ext.grid.RowSelectionModel({}),
////			autoHeight : true
//			height:200//用autoHeight不能显示scroll bar
////			bbar: new Ext.PagingToolbar({
////	            pageSize: 10,
////	            store: ds,
////	            displayInfo: true,
////	            displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
////	            emptyMsg: "没有记录"
////        })
//		}));
		Ext.getCmp("panelGrid").add(eGridPanel);

//	//UDP绩效结果折线图
	Ext.getCmp("panelChart").add(evaChart);
	
	//	绩效对比结果Grid
	Ext.getCmp("panelCompare").add(new Ext.grid.GridPanel({
			id : "compareGridPanel",
			region : "east",
			width:500,
			store : new Ext.data.Store(),
			columns : [{
				width : 50,
				sortable : true,
//				dataIndex : "Ia",
				dataIndex : "lambdaA",
				header : "攻击强度(次/s)"
			}, {
				width : 50,
				sortable : true,
				dataIndex : "udpT",
				header : "UDP攻击平均停留时间(s)"
			}, {
				width : 50,
				sortable : true,
				dataIndex : "udpE",
				header : "攻击绩效(s)"
			}, 	{
				width : 50,
				sortable : true,
				dataIndex : "synPb",
				header : "SYN Flood情况下的拒绝服务概率"
			}, 
			{
				width : 50,
				sortable : true,
				dataIndex : "synE",
				header : "SYN Flood攻击绩效(s)"
			}],
			autoWidth : true,
			selModel : new Ext.grid.RowSelectionModel({})	
			}));
		
//	//绩效对比折线图
	Ext.getCmp("comparePanelChart").add(new Ext.chart.LineChart({
		store:Ext.getCmp("compareGridPanel").store,
		xField:'lambdaA',
//		xField:'Ia',
		series:[
			{
			displayName: 'UDP Flood攻击绩效',
			yField:'udpE'
		},{
			displayName: 'SYN Flood攻击绩效',
			yField:'synE'
		}
		]
	}
	));
	

	
//tab切换
Ext.getCmp("tabpanelDoS").on("tabchange", function(tabPanel,selectedTab){
	if (eGridPanel.rendered) {
					evaStore.removeAll();
					if (selectedTab.title == "UDP Flood") {
						if (Ext.getCmp("radioUDPW").checked) {
							eGridPanel.reconfigure(evaStore, udpWColModel);
						} else {// radioUDPH

						}
					} else if (selectedTab.title == "SYN Flood") {
						if (Ext.getCmp("radioUDPW").checked) {
							eGridPanel.reconfigure(evaStore, synWColModel);
						}
					}//SYN Flood
				}
});
//
//Ext.getCmp("panelUDP").on("activate", function(){
//    evaStore.removeAll();
//	if (Ext.getCmp("radioUDPW").checked) {
////			eGridPanel.reconfigure(evaStore, udpWColModel);
//////    } else if(Ext.getCmp("radioUDPH").checked){
//////		;
//	}
//////	if(eGridPanel.rendered){
//////		eGridPanel.getView().refresh();
//////	}
//});
//Ext.getCmp("panelSYN").on("activate", function(){
//    evaStore.removeAll();
//	if (Ext.getCmp("radioSYNW").checked) {
//			eGridPanel.reconfigure(evaStore, synWColModel);
//    } else if(Ext.getCmp("radioSYNH").checked){
//		;
//	}
//////	if(eGridPanel.rendered){
//////		eGridPanel.getView().refresh();
//////	}
//});


//RadioButton
	Ext.getCmp("radioUDPW").on("check", function() {
		if(this.checked) {
//			Ext.getCmp("synFormW").hide();
//			Ext.getCmp("udpFormW").show();
			evaStore.removeAll();
			eGridPanel.reconfigure(evaStore, udpWColModel);
			if(eGridPanel.rendered){
				eGridPanel.getView().refresh();
			}
		}
});
	Ext.getCmp("radioSYNW").on("check", function() {
		if(this.checked) {
//			Ext.getCmp("udpFormW").hide();
//			Ext.getCmp("synFormW").show();
			evaStore.removeAll();
			eGridPanel.reconfigure(evaStore, synWColModel);//TODO
			if(eGridPanel.rendered){
				eGridPanel.getView().refresh();
//				eGridPanel.getEl().render();
//				eGridPanel.render();
			}
		}
});

////UDP
//udp计算
Ext.getCmp("udpButtonCalc").on("click", function() {
			if (Ext.getCmp("radioUDPW").getValue()) {
				udpW();
			} else if (Ext.getCmp("radioUDPH").getValue()) {
				udpH();
			}
		});
// 保存当前UDP计算结果
	Ext.getCmp("udpButtonSave").on("click", function() {
				if (Ext.getCmp("radioUDPW").getValue()) {
					saveUdpW();
				} else if (Ext.getCmp("radioUDPH").getValue()) {
					saveUdpH();
				}
			});// buttonSave on click
// 显示udp历史记录
	Ext.getCmp("udpButtonListHistory").on("click", function() {
				if (Ext.getCmp("radioUDPW").getValue()) {
					listHistoryUdpW();
				} else if (Ext.getCmp("radioUDPH").getValue()) {
					listHistoryUdpH();
				}
			});// buttonListHistory on click

	Ext.getCmp("udpButtonTest").on("click", function() {
//				eGridPanel.getSelectionModel().getSelected();
				var selectedRows=eGridPanel.getSelectionModel().getSelections();//获取所有选中行，
                for (var i = 0; i < selectedRows.length; i++) {
//                    str = str + rows[i].get('id') + 'E';
					alert(selectedRows[i].get('Eva_Time'));
                } 
				Ext.getCmp("test").setText('<b>New</b> value...', false);
		//				alert("d");
			});// buttonListTest on click

//syn计算
Ext.getCmp("synButtonCalc").on("click", function() {
			if (Ext.getCmp("radioSYNW").getValue()) {
				synW();
			} else if (Ext.getCmp("radioSYNH").getValue()) {
				synH();
			}
		});
// 保存当前SYN计算结果
	Ext.getCmp("synButtonSave").on("click", function() {
				if (Ext.getCmp("radioSYNW").getValue()) {
					saveSynW();
				} else if (Ext.getCmp("radioSYNH").getValue()) {
					saveSynH();
				}
			});// buttonSave on click
// 显示syn历史记录
	Ext.getCmp("synButtonListHistory").on("click", function() {
				if (Ext.getCmp("radioSYNW").getValue()) {
					listHistorySynW();
				} else if (Ext.getCmp("radioSYNH").getValue()) {
					listHistorySynH();
				}
			});// buttonListHistory on click


//	攻击绩效计算
//	Ext.getCmp("buttonCalc").on("click", function() {
//				if (Ext.getCmp("radioUDPW").getValue()) {
//					udpW();
//				} else if (Ext.getCmp("radioSYNW").getValue()) {
//					synW();
//				} else if (Ext.getCmp("radioUDPH").getValue()) {
//				} else if (Ext.getCmp("radioSYNH").getValue()) {
//				}
//			});// buttonCalc on click
			
// 保存当前计算结果
//	Ext.getCmp("buttonSave").on("click", function() {
//				if (Ext.getCmp("radioUDPW").getValue()) {
//					saveUdpW();
//				} else if (Ext.getCmp("radioSYNW").getValue()) {
//					saveSynW();
//				} else if (Ext.getCmp("radioUDPH").getValue()) {
//				} else if (Ext.getCmp("radioSYNH").getValue()) {
//				}
//			});// buttonSave on click
// 显示历史记录
//	Ext.getCmp("buttonListHistory").on("click", function() {
//				if (Ext.getCmp("radioUDPW").getValue()) {
//					listHistoryUdpW();
//				} else if (Ext.getCmp("radioSYNW").getValue()) {
//					listHistorySynW();
//				} else if (Ext.getCmp("radioUDPH").getValue()) {
//				} else if (Ext.getCmp("radioSYNH").getValue()) {
//				}
//			});// buttonListHistory on click

////SYN Flood攻击绩效计算
//	Ext.getCmp("synButtonCalc").on("click", function() {
//		Ext.getCmp("synTextPb").setValue("正在计算...");
//
//		var synM = parseFloat(Ext.getCmp("synTextM").getValue());
//		var synSa = parseFloat(Ext.getCmp("synTextSa").getValue());
//		var synDelay = parseFloat(Ext.getCmp("synTextDelay").getValue());
//		var synIr = parseFloat(Ext.getCmp("synTextIr").getValue());
//		var synIa = parseFloat(Ext.getCmp("synTextIa").getValue());
//		var synPb = synCalcPb(synM, synSa, synDelay * 2,synIr, synIa);
//		var synTi = parseFloat(Ext.getCmp("synTextTi").getValue());
//		var synE = calcE(0,synPb, synTi);//TODO
//		Ext.getCmp("synTextPb").setValue(synPb.toString());
//	});

//绩效对比
	Ext.getCmp("compareButtonCalc").on("click", function() {
//		Ext.getCmp("compareTextPb").setValue("正在计算...");
		Ext.getCmp("compareGridPanel").store.removeAll();
		var compareUdpT = 0.0,compareUdpTt=0.0, compareUdpE=0.0, compareSynPb=0.0,compareSynE=0.0;

		var compareM = parseFloat(Ext.getCmp("compareTextM").getValue());
		var compareSynSa = parseFloat(Ext.getCmp("compareTextSynSa").getValue());
//		var synDelay = parseFloat(Ext.getCmp("compareTextDelay").getValue());
		var compareIr = parseFloat(Ext.getCmp("compareTextIr").getValue());
//		var synIa = parseFloat(Ext.getCmp("compareTextIa").getValue());
		var compareLp = parseFloat(Ext.getCmp("compareTextLp").getValue());
		var compareBw = parseFloat(Ext.getCmp("compareTextBw").getValue());

		var compareLt = parseFloat(Ext.getCmp("compareTextLt").getValue());//一次会话的传输总量
		var compareTi = parseFloat(Ext.getCmp("compareTextTi").getValue());
		var compareSynSr = 2*40*8/compareBw/1000000;//SYN握手包长40Bytes
		var compareSynTt = compareLt*8/compareBw/1000000;
	
		var compareMinLambdaA =parseFloat(Ext.getCmp("compareTextMinLambdaA").getValue()); 	
		var compareMaxLambdaA =parseFloat(Ext.getCmp("compareTextMaxLambdaA").getValue());
		if(compareMinLambdaA>compareMaxLambdaA){
			var tmp = compareMinLambdaA;
			compareMinLambdaA = compareMaxLambdaA;
			compareMaxLambdaA = tmp;
		}
		var step = (compareMaxLambdaA-compareMinLambdaA)/50;
		for(var compareLambdaA=compareMinLambdaA;compareLambdaA<=compareMaxLambdaA;compareLambdaA+=step){
			compareUdpT = udpCalcT(compareLp, compareBw, compareIr, 1/compareLambdaA);
			compareUdpTt = compareUdpT*compareLt*1000000/compareLp;
			compareUdpE = calcE(compareUdpTt, 0, 0);
			compareSynPb = synCalcPb(compareM, compareSynSa, compareSynSr, compareIr, 1/compareLambdaA);
//			compareSynPb = synCalcPb(compareM, compareSynSa, compareSynSr, compareIr, compareLambdaA);
			compareSynE = calcE(compareSynTt, compareSynPb, compareTi);
			Ext.getCmp("compareGridPanel").store.add(new resultRecord({lambdaA:compareLambdaA,Ia:1/compareLambdaA, udpT:compareUdpT, synPb:compareSynPb, udpE:compareUdpE, synE:compareSynE}));
		}
	});






//博弈
	Ext.getCmp("gameButtonCalc").on("click", function() {
		Ext.getCmp("gameTextMatrix").setValue("正在计算...\n");

		var gameLp = parseFloat(Ext.getCmp("gameTextLp").getValue());
//		var gameBw = parseFloat(Ext.getCmp("gameTextBw").getValue());
//		var gameM = parseFloat(Ext.getCmp("gameTextM").getValue());
		var gameSa = parseFloat(Ext.getCmp("gameTextSa").getValue());
//		var gameDelay = parseFloat(Ext.getCmp("gameTextDelay").getValue());
		var gameIr = parseFloat(Ext.getCmp("gameTextIr").getValue());
		var gameLt = parseFloat(Ext.getCmp("gameTextLt").getValue());//一次会话的传输总量

		var gameTi = parseFloat(Ext.getCmp("gameTextTi").getValue());
		var gameZ = parseInt(Ext.getCmp("gameTextZ").getValue());//攻击方拥有的傀儡主机数
		var gameRudp = parseFloat(Ext.getCmp("gameTextRudp").getValue());//每台傀儡主机可发起的UDP Flood攻击强度(次/s)
		var gameRsyn = parseFloat(Ext.getCmp("gameTextRsyn").getValue());//每台傀儡主机可发起的SYN Flood攻击强度(次/s)
		var gameI = parseFloat(Ext.getCmp("gameTextI").getValue());//防护方总投入
		var gameUq = parseFloat(Ext.getCmp("gameTextUq").getValue());//半开队列单位成本
		var gameUb = parseFloat(Ext.getCmp("gameTextUb").getValue());//带宽单位成本
		
		
//计算绩效:
		var maxAttackE = 0, minDefenceLost = Infinity, selM=1, selU=0;
		for(var m=1; m<=parseInt(gameI/gameUq); m++){
			Ext.getCmp("gameTextMatrix").setValue(Ext.getCmp("gameTextMatrix").getValue()+"m="+m.toString()+" ");
			for(var gameU=0;gameU<=gameZ;gameU++){		
				var gameUdpBw = (gameI - m*gameUq)/gameUb;		
				var gameUdpIa = 1/gameU/gameRudp;
				var gameSynIa = 1/(gameZ-gameU)/gameRsyn;
				//UDP Flood:
				var gameUdpT = udpCalcT(gameLp, gameUdpBw, gameIr, gameUdpIa);
				//一次会话的平均停留总时间Tt
//				if(gameUdpT == "∞"){
//					gameUdpE = "∞";
//				}
//				else{
					var gameUdpTt = gameUdpT*gameLt*1000000/gameLp;					
//				}


				//SYN Flood:
				var gameSynSr = 2*40*8/gameUdpBw/1000000;//SYN包长度40Bytes
				var gameSynPb = synCalcPb(m, gameSa, gameSynSr, gameIr, gameSynIa);
				var gameE = calcE(gameUdpTt, gameSynPb, gameTi);//计算混合攻击绩效
				
				//选择策略
                if (gameU==0 || gameE > maxAttackE) {
                    maxAttackE = gameE;
                    selU = gameU;
                }
				
//				Ext.getCmp("gameTextMatrix").setValue(Ext.getCmp("gameTextMatrix").getValue()+"u="+gameU.toString()+","+gameUdpE..toString()+","+gameSynE.toPrecision(3).toString()+ " ");
				Ext.getCmp("gameTextMatrix").setValue(Ext.getCmp("gameTextMatrix").getValue()+"u="+gameU.toString()+","+gameE.toString().substring(0,5)+ " ");
			}
			Ext.getCmp("gameTextMatrix").setValue(Ext.getCmp("gameTextMatrix").getValue()+"攻击方选择U="+selU+",收益"+maxAttackE+"\n");
            
            if (maxAttackE < minDefenceLost) {
                minDefenceLost = maxAttackE;
                selM = m;
            }

			
		}//for	

//
//		var gameIa = parseFloat(Ext.getCmp("gameTextIa").getValue());
//		var gameE = calcE(0,gamePb, gameTi);
//		Ext.getCmp("synTextPb").setValue(synPb.toString());

//		Ext.getCmp("gameTextMatrix").setValue("...");	
		Ext.getCmp("gameTextMatrix").setValue(Ext.getCmp("gameTextMatrix").getValue()+"防护方选择M="+selM+",损失"+minDefenceLost+"\n计算完成!");

});//Ext.getCmp("gameButtonCalc").on("click"
	p.render("ddd");

//Window用法
//	var p = new system.w();
//	p.show();
});//Ext.onReady

//Ext.onReady(function(){
//    var pan = new Ext.Panel({
//		renderTo:'ddd',
//	
//	
//		
//	
//})